package 字符串解码;

import java.util.LinkedList;

//栈解法
public class Solution {
    public String decodeString(String s) {
        StringBuilder res = new StringBuilder();
        LinkedList<Integer> stack_multi = new LinkedList<>();
        LinkedList<String> stack_res = new LinkedList<>();
        int multi = 0;
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if(c >= '0'&& c <= '9') multi = multi*10 + Integer.parseInt(c+"");
            else if(c == '['){
                stack_multi.addLast(multi);
                stack_res.addLast(res.toString());
                multi = 0;
                res = new StringBuilder();
            }
            else if(c==']'){
                StringBuilder temp = new StringBuilder();
                int cur_multi = stack_multi.removeLast();
                for (int i = 0; i < multi; i++) {
                    temp.append(res);
                }
                res = new StringBuilder(stack_res.removeLast()+temp);

            }
            else res.append(c);

        }
        return res.toString();

    }



}
